#!/usr/bin/perl
#Programmer: Rory Carmichael
#Purpose: Use the charts api to make me a boxplot on top of a bar chart.  BAM -- deprecated due to chart api
#Usage: g_plots.pl output_prefix inputfile

use List::Util qw(max);

my %real;
my %rand;
my $chartsize = 500;

my $mode = 0;
while (my $line = <>) {
	chomp $line;
	my @splitline = split(/\t/, $line);
	if ($mode == 0 && $line !~ /:/) {
		$real{$splitline[0]} = $splitline[1];
	} elsif ($line =~ /mean boxes/) {
		$mode = 1;
	} elsif ($mode == 1 && line !~ /:/) {
		my $index = shift(@splitline);
		$rand{$index} = join(",",@splitline);
#		print join(",",@splitline) . "\n";
	}
}

my $real_size = scalar(keys(%real));
my $rand_size = scalar(keys(%rand));
my $m = max($real_size,$rand_size);

my @bar, @min, @q1, @q2, @q3, @max;
for (my $i=0;$i<$m;$i++) {
	if(!exists($real{$i})) {
		push(@bar, 0);
	} else {
		push(@bar, $real{$i});
	}
	if(!exists($rand{$i})) {
		push(@min, -1);
		push(@q1, -1);
		push(@q2, -1);
		push(@q3, -1);
		push(@max, -1);
	} else {
		my @split = split(/,/, $rand{$i});
		push(@min, shift(@split));
		push(@q1, shift(@split));
		push(@q2, shift(@split));
		push(@q3, shift(@split));
		push(@max, shift(@split));
	}
}

my $url = "http://chart.googleapis.com/chart?chs=" . $chartsize . "&chds=a&chf=bg,s,65432100&chxt=x,x,y,y&cht=bvg&chxl=1:|Overlap|3:|Positions&chxp=1,50|3,200&chco=C6D9FD&chd=t1:" . join("|", join(",",@bar,-1), join(",",@min,-1), join(",",@q1,-1), join(",",@q3,-1), join(",",@max,-1) , join(",",@q2,-1)) . "&chm=F,FF9900,1,0:$m,10|H,0CBF0B,1,0:$m,1:10|H,000000,5,0:$m,1:10|H,0000FF,4,0:$m,1:10";
#print "$url\n";
print `wget '$url' -O -`;
